package com.lemon.smartsafety.ui.activity.user;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.HttpPath;
import com.lemon.smartsafety.result.OldBaseMode;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseActivity;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.JsonUtils;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.SystemUtil;
import com.lemon.smartsafety.utils.app.SharedPreferencesServer;
import com.lemon.smartsafety.utils.app.ToastUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.rengwuxian.materialedittext.MaterialEditText;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 验证用户是否绑定邮箱
 */
public class VerifyUserEmailActivity extends NewBaseActivity {

    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @Bind(R.id.metRemarkEmail)
    MaterialEditText metRemarkEmail;
    @Bind(R.id.txt_verCode)
    TextView txtVerCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getBundleArgs() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_verify_user_email;
    }

    @Override
    protected void initTitleView() {
        setTitle("验证用户");
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initHttp() {

    }

    @Override
    protected void initList() {

    }

    @Override
    protected void loadList() {

    }

    @Override
    protected void refreshView() {

    }

    @Override
    protected void callFinish() {
        mActivity.finish();
    }


    @OnClick({R.id.txt_verCode})
    public void onSubmitClick(View view) {
        switch (view.getId()) {
            case R.id.txt_verCode:
                SystemUtil.HideKeyboard(mActivity);
                if (!StringUtils.isEmpty(metRemarkEmail.getText().toString().trim())) {
                    httpPostEmailVerCode();
                }else{
                    ToastUtil.show("用户名不能为空");
                }
                break;

        }

    }




    /**
     *  发送邮箱验证码
     */
    private void httpPostEmailVerCode() {

        LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");
        params.put("login_name", metRemarkEmail.getText().toString().trim());
        params.put("type", "email");

        showLoading("");

        SharedPreferencesServer mSharedPreferencesServer = new SharedPreferencesServer(this);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(HttpPath.Find_user_password);

        HttpOkBase.httpBasePost(params, serverUrl, "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {

                dismissLoading();
                if (httpClient) {
                    OldBaseMode oldBaseMode = JsonUtils.fromJson(result, OldBaseMode.class);
                    if (!CommonUtil.objIsEmpty(oldBaseMode)) {


                        if (!oldBaseMode.isIsError()) {
                            mSharedPreferencesHelper.setBindLoginName(metRemarkEmail.getText().toString().trim());
                            LogUtils.LOGE("验证码已发送到邮箱");
                            UIHelper.showActivity(mActivity, ResetPassWordEmailActivity.class);
                            finish();
                        }else{
                            ToastUtil.show(oldBaseMode.getError().getMsg());
                            if(!oldBaseMode.getError().getMsg().equals("用户不存在")){
                                mSharedPreferencesHelper.setBindLoginName(metRemarkEmail.getText().toString().trim());
                                UIHelper.showActivity(mActivity, BindEmailForLoginActivity.class);
                            }
                        }

                    }
                }
            }
        });


    }





}
